﻿using System;
using System.Globalization;
using System.Windows.Forms;
using Campus.Main.Basic.InsertValues;
using cpmntctrl;
using dbcntn;

namespace Campus.Main.Basic.Settings
{
    public partial class Ceza : DefaultForm
    {
        private string StudentId { get; set; }
        //public static string ConnectionString = "server=ef.manas.kg;User Id=admin;Persist Security Info=True;database=Ogrenci2006_dbo;password=password";
        public string ID { get; set; }
        public string ID1 { get; set; }
        public Ceza(string studentID)
        {
            InitializeComponent();
            StudentId = studentID;
            aToDb=new ConnectionToDb(Helper.ConnectionString);
            toolStripStatusLabel1.Text = studentID;
            toolStripStatusLabel3.Text = "Ceza sayisi=" + aToDb.SelectField("select count(ID) from disiplin where student_id like('" + studentID + "')");
        }
        public override void Initialize()
        {
            ID = ID1 = null;
            data.Clear();
            aToDb.AddItem(cmb_ceza,"ceza");
            textBox2.Text =
                aToDb.SelectField(
                    "select sum(a.ball) from ceza a,disiplin b where a.ID=b.ceza and b.student_id like('" + StudentId +
                    "')");
            aToDb.AddItemDBGridView(dataGridView1,Query.MaddeNo);

            string s = Query.MaddeBend + " and a.maddeno_id=" + 1;
            aToDb.AddItemDBGridView(dataGridView2, s);
            aToDb.AddItemDBGridView(dataGridView3,Query.Disiplin+" where student_id like('"+StudentId+"')");
            
            ControlComponent.UnVisibleCells(dataGridView1,0);
            ControlComponent.UnVisibleCells(dataGridView2, 0,1);
            ControlComponent.UnVisibleCells(dataGridView3,0);
        }
        public override void Bind(object sender, FormClosedEventArgs e)
        {
            Initialize();
        }
        private void Ceza_Load(object sender, EventArgs e)
        {
            Initialize();
        }

        private void dataGridView1_CellClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
        {
            if (e.RowIndex==-1||e.ColumnIndex==-1)
                return;
            ID = Convert.ToString(dataGridView1[0, e.RowIndex].Value);
            string s = Query.MaddeBend + " and a.maddeno_id=" + ID;
            aToDb.AddItemDBGridView(dataGridView2, s);
            
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            MaddeNo f=new MaddeNo();
            f.FormClosed += Bind;
            f.ShowDialog();

        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow != null && dataGridView2.CurrentRow != null)
            {
                string maddeno = Convert.ToString(dataGridView1[1, dataGridView1.CurrentRow.Index].Value);
                string maddebend = Convert.ToString(dataGridView2[3, dataGridView2.CurrentRow.Index].Value);
                string maddetext = Convert.ToString(dataGridView2[4, dataGridView2.CurrentRow.Index].Value);
                string aciklama = richTextBox1.Text;
                string ceza_id = Convert.ToString(cmb_ceza.SelectedValue);
                DateTime d1 = Convert.ToDateTime(dateTimePicker1.Text);
                string date = d1.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
                aToDb.InsertIntoTableDisiplin(StudentId, maddeno, maddebend, maddetext, aciklama, ceza_id, date);
                MessageBox.Show("Eklendi");
                aToDb.AddItemDBGridView(dataGridView3, Query.Disiplin+" where student_id like('"+StudentId+"')");
                toolStripStatusLabel3.Text =
                    aToDb.SelectField("select count(id) from disiplin where student_id like('" + StudentId + "')");
                textBox2.Text =
                    aToDb.SelectField(
                        "select sum(a.ball) from ceza a,disiplin b where a.ID=b.ceza and b.student_id like('" +
                        StudentId + "')");
                int ball =
                    Int32.Parse(
                        aToDb.SelectField("select ball from onmaintable where student_id like('" + StudentId + "')"));
                ball += Int32.Parse(textBox2.Text);
                aToDb.UpdateMainTable("ball", ball, StudentId);
                //aToDb.UpdateMainTable("ceza",Int32.Parse(textBox2.Text),StudentId);
            }


        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Cezayi silmek istiyourmusunuz???", "Dikkat", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                if (dataGridView3.CurrentRow != null)
                {
                    string id = Convert.ToString(dataGridView3[0, dataGridView3.CurrentRow.Index].Value);
                    aToDb.DeleteTable("disiplin", "id", id);
                    aToDb.AddItemDBGridView(dataGridView3, Query.Disiplin+" where student_id like('" + StudentId + "')");
                    toolStripStatusLabel3.Text =
                        aToDb.SelectField("select count(id) from disiplin where student_id like('" + StudentId + "')");
                    int ball =
                        Int32.Parse(
                            aToDb.SelectField("select ball from kmaintable where student_id like('" + StudentId + "')"));
                    ball -= Int32.Parse(textBox2.Text);
                    textBox2.Text =
                        aToDb.SelectField(
                            "select sum(a.ball) from ceza a,disiplin b where a.ID=b.ceza and b.student_id like('" +
                            StudentId + "')");
                    aToDb.UpdateKMainTable("ball", ball, StudentId);
                }
            }
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex==-1||e.ColumnIndex==-1)
                return;
            ControlComponent.AddToList(data, dataGridView1, e, 0, 1);
            MaddeNo f = new MaddeNo(data);
            f.FormClosed += Bind;
            f.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Silmek istiyour musunuz??","Dikkat",MessageBoxButtons.YesNo)==DialogResult.Yes)
                aToDb.DeleteTable("maddeno","id",ID);
            Initialize();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MaddeBend f=new MaddeBend();
            f.FormClosed += Bind;
            f.ShowDialog();
        }

        private void dataGridView2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1 || e.ColumnIndex == -1)
                return;
            ControlComponent.AddToList(data, dataGridView2, e, 0, 1,3,4);
            MaddeBend f = new MaddeBend(data);
            f.FormClosed += Bind;
            f.ShowDialog();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (ID1!=null)
            {
                if (MessageBox.Show("Silmek istiyour musunuz??", "Dikkat", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    aToDb.DeleteTable("maddebend","ID",ID1);
                Initialize();
            }
        }

        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1 || e.ColumnIndex == -1)
                return;
            ID1 = Convert.ToString(dataGridView2[0, e.RowIndex].Value);
        }

        private void dataGridView1_Enter(object sender, EventArgs e)
        {
            
        }

    }
}
